
var DB;

/*
 * Initialize the local DB
 */
	function initDB () {

	try {

		//Check whether this browser supports local databases
		if(!window.openDatabase) alert("Uw browser ondersteund geen local databases. Gebruik Chrome!");
		else {

			//Params for local DB
			var shortName = 'DB';
			var version = '1.0';
			var displayName = 'DB Web & Mobile';
			var maxsize = 100000;

			//Create the DB with above params
			DB = openDatabase(shortName, version, displayName, maxsize);

			dropAllTables();
			//Create all tables
			createAllTables();

			//Select all data
			//selectAll();
		}

	}
	catch (e) {

		if (e == 2) {
			// Version number mismatch.
			console.log("Invalid database version.");
		} else {
			console.log("Unknown error "+e+".");
		}
		return;

	}

   }


   /*
	* Create all the tables
	*/
   function createAllTables() {

	DB.transaction(
		function (t){

			// Users table
			t.executeSql("CREATE TABLE IF NOT EXISTS users(userId INTEGER NOT NULL PRIMARY KEY, name VARCHAR(100) NOT NULL, age VARCHAR(10) NOT NULL)", []);
		}
	);
		//prePopulate();
   }

	/*
	 * Empty tables
	 */
	function emptyAllTables() {

		DB.transaction(
			function(t){
				t.executeSql("DELETE FROM users");
			}
		);
	}


	function dropAllTables() {

		DB.transaction(

			function(t) {

				t.executeSql("DROP TABLE users20");
				t.executeSql("DROP TABLE users3");

			}

		);

	}


	/*
	 * Add one user
	 */
	function addUser(user){

		DB.transaction(
			function(t) {

				t.executeSql("INSERT INTO users (name, age) VALUES (?, ?)", [user['name'], user['age']]);
			}
		);

	}

$(document).ready(function(){

	$("#userButton").click(function(){
		var user = Array();
		user['name'] = $("#name").val();
		user['age'] = $("#age").val();

		addUser(user);

		/* Local Storage
		localStorage.setItem('currentUser', document.getElementById('name').value);
		var currentUser = localStorage.getItem('currentUser');
		console.log(currentUser);
		*/


	});


	$("#emptyDB").click(function(){
		emptyAllTables();
	});
});







	/*
	* Fill the tables with data
	*/

	/*function prePopulate() {

		DB.transaction(
			function(t) {
				var data = ['Martijn Willemse', '21'];
				t.executeSql("INSERT INTO users (name, age) VALUES (?, ?)", [data[0], data[1]]);
			}
		);

   }*/